package recursive_descent_parser;

public class SimpleParser {
	
	String expr;
	
	public SimpleParser(String expression){
		expr=expression;
	}
	
	public float expressionValue(int i){
		
		float value=0;
		float leftN=0;
		float rightN=0;
		char op = 0;
		char x=expr.charAt(i);
		
		if(isDigit(x)){
			System.out.println(x-48);
			System.exit(0);///////////////////
			return (x-48);
		}
		else if(x=='('){
			int j=i;
			while(expr.charAt(j)!=')')
				j++;
			leftN=expressionValue(i++);
			if(j+1<expr.length())
				op=expr.charAt(j+1);
			if(j+2<expr.length())
				rightN=expressionValue(j+2);
		}
		else if(x==')')
			return value;
		
		switch(op){
		case '+':
			value=leftN+rightN;
		case '-':
			value=leftN-rightN;
		case '*':
			value=leftN*rightN;
		case '/':
			value=leftN/rightN;
		}
		
		return value;
	}
	
	private boolean isDigit(int n){
		switch (n){
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 0:
			return true;
		default:
			break;
		}
		return false;
	}
	
	
	public static void main(String args[]){
		
		SimpleParser s=new SimpleParser("(3+5)");
		float res=s.expressionValue(0);
		System.out.println(res);
		
		
		
	}

}
